package lumingweihua.future.cn.lumingweihua.ui.mine;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zhusx.core.interfaces.IHttpResult;
import com.zhusx.core.network.HttpRequest;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import lumingweihua.future.cn.baselibgxh.dialog.YesOrNoDialog;
import lumingweihua.future.cn.baselibgxh.dialog.YesOrNoDialogEntity;
import lumingweihua.future.cn.lumingweihua.R;
import lumingweihua.future.cn.lumingweihua.network.LoadData;
import lumingweihua.future.cn.lumingweihua.network.LoadingHelper;
import lumingweihua.future.cn.lumingweihua.network.SimpleHttpListener;
import lumingweihua.future.cn.lumingweihua.ui.BaseCompatActivity;
import lumingweihua.future.cn.lumingweihua.ui.mine.domain.CarDetailData;
import lumingweihua.future.cn.lumingweihua.widget.FrescoImageView;

/**
 * todo 车辆详情
 * Created by z1986 on 2018/3/17.
 */

public class CarDetailActivity extends BaseCompatActivity {
    @BindView(R.id.tv_message)
    TextView messageTv;
    @BindView(R.id.iv_image)
    ImageView imageView;
    @BindView(R.id.layout_nar)
    ViewGroup narLayout;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.tv_license)
    TextView tvLicense;
    @BindView(R.id.tv_type)
    TextView tvType;
    @BindView(R.id.tv_count)
    TextView tvCount;
    @BindView(R.id.layout_content)
    LinearLayout layoutContent;
    @BindView(R.id.iv_driver)
    FrescoImageView ivDriver;
    @BindView(R.id.iv_car)
    FrescoImageView ivCar;
    @BindView(R.id.tv_modify_phone)
    TextView tvModifyPhone;
    @BindView(R.id.tv_delete)
    TextView tvDelete;
    CarDetailData en;
    String id;
    final int REQUEST_ID = 87;
    LoadData<String> deleteData;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mine_car_detail);
        ButterKnife.bind(this);
        id = getIntent().getStringExtra(_EXTRA_String_ID);
        deleteData = new LoadData<>(LoadData.Api.个人中心车辆删除, this);
        deleteData._setOnLoadingListener(new SimpleHttpListener<String>() {
            @Override
            public void onLoadComplete(LoadData.Api api, HttpRequest<Object> httpRequest, IHttpResult<String> result) {
                showToast(result.getData());
                setResult(RESULT_OK);
                finish();
            }
        });
        LoadData<CarDetailData> loadData = new LoadData<>(LoadData.Api.微信端车辆详情, this);
        loadData._setOnLoadingListener(new LoadingHelper<CarDetailData>(layoutContent, loadData) {
            @Override
            public void __onComplete(HttpRequest<Object> request, IHttpResult<CarDetailData> data) {
                initView(data.getData());
            }
        });
        loadData._loadData(id);
    }

    private void initView(CarDetailData en) {
        this.en = en;
        tvName.setText(en.car_name);
        tvPhone.setText(en.car_mobile);
        tvLicense.setText(en.license);
        tvType.setText(en.type);
        tvCount.setText(en.load);
        switch (String.valueOf(en.car_status)) {
            case "1":
                messageTv.setText("车辆信息审核中,请耐心等待");
                messageTv.setTextColor(getResources().getColor(R.color.font_blue));
                imageView.setImageResource(R.drawable.iv_shenhe);
                narLayout.setVisibility(View.GONE);
                tvModifyPhone.setVisibility(View.INVISIBLE);
                break;
            case "2":
                messageTv.setText("审核通过,该车辆可以在平台进行抢单");
                messageTv.setTextColor(Color.parseColor("#59b469"));
                imageView.setImageResource(R.drawable.iv_tongguo);
                tvDelete.setVisibility(View.GONE);
                break;
            case "3":
                messageTv.setText("审核不通过:\n" + en.reason);
                messageTv.setTextColor(Color.RED);
                narLayout.setVisibility(View.VISIBLE);
                imageView.setImageResource(R.drawable.iv_shibai);
                tvModifyPhone.setVisibility(View.INVISIBLE);
                break;
        }
        ivDriver.setImageURI(en.drive_img);
        ivCar.setImageURI(en.run_img);
    }

    @OnClick({R.id.tv_modify_phone, R.id.tv_modify_info, R.id.tv_delete})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_modify_phone:
                startActivityForResult(new Intent(v.getContext(), ModifyPhoneActivity.class).putExtra(_EXTRA_String_ID, id), REQUEST_ID);
                break;
            case R.id.tv_modify_info:
                startActivityForResult(new Intent(v.getContext(), ModifyCarInfoActivity.class).putExtra(_EXTRA_String_ID, id).putExtra(_EXTRA_Serializable, en), REQUEST_ID);
                break;
            case R.id.tv_delete:
                new YesOrNoDialog(v.getContext(), new YesOrNoDialogEntity("是否确认删除车辆信息?", "取消", "删除"),
                        new YesOrNoDialog.OnYesOrNoDialogClickListener() {
                            @Override
                            public void onYesOrNoDialogClick(YesOrNoDialog.YesOrNoType yesOrNoType) {
                                switch (yesOrNoType) {
                                    case BtnOk:
                                        deleteData._loadData(id);
                                        break;

                                    default:
                                        break;
                                }
                            }
                        }).show();
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_ID && resultCode == RESULT_OK) {
            recreate();
        }
    }
}
